# @shuaige : 陈世玉
# @name :08站长素材.py
# @time :2024/12/4 19:48
#1.请求对象的定制
#2.解析源码
#3.下载图片（下载前十页图片）
#https://a2put.chinaz.com/slot/callback?id=s1694629161422034&fromUrl=https://sc.chinaz.com/tupian/meinvtupian_2.html
import requests
from lxml import etree
import shutil
def get_url(page):
        if(page==1):
            url='https://sc.chinaz.com/tupian/meinvtupian.html'
        else:
            i=str(page)
            url='https://sc.chinaz.com/tupian/meinvtupian_'+i+'.html'
        return url
def get_request(url) :
    headers={
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36 Edg/108.0.1462.54'
    }
    response=requests.get(url,headers=headers)
    response.encoding='utf-8'
    # print('状态码：',response.status_code)
    # print(response.text)
    return response.text
def get_img(html):
    tree=etree.HTML(html)
    # print(tree)
    img_list=tree.xpath('//div[@class="tupian-list com-img-txt-list"]//div/img/@data-original')
    name_list=tree.xpath('//div[@class="tupian-list com-img-txt-list"]//div/img/@alt')
    # print(len(img_list))
    # print(len(name_list))
    for i in range(len(img_list)):
        name=name_list[i]
        src=img_list[i]
        url='http:'+src
        # print(name,url)
        # rc=requests.get(url)
        # with open('img/'+name+'.jpg','wb') as f:
        #     f.write(rc.content)
        with requests.get(url, stream=True) as r:
            with open('img/'+name+'.jpg', 'wb') as f:
                shutil.copyfileobj(r.raw, f)


if __name__=='__main__':
    page=int(input('请输入页数：'))
    for i in range(1,page+1):
        url=get_url(page)
        content=get_request(url)
        # print(content)
        print('正在爬取第'+str(i)+'页')
        get_img(content)